const env = require("../config/ali-oss");
const Base64 = require("./Base64.js");

require("./hmac.js");
require("./sha1.js");
const Crypto = require("./crypto.js");

const uploadFile = function (params) {
	console.log(params, "paramsparams");
	if (!params.filePath) {
		wx.showModal({
			title: "文件错误",
			content: "请重试",
			showCancel: false,
		});
		return;
	}

	const aliyunFileKey =
		params.dir +
		params.filePath.substring(params.filePath.lastIndexOf("/") + 1);
	const aliyunServerURL = env.uploadImageUrl;
	// const accessid = params.voucher.accessId;
	// const policyBase64 = params.voucher.policy;
	// const signature = params.voucher.signature;
	const accessid = env.OSSAccessKeyId;
	const policyBase64 = getPolicyBase64();
	const signature = getSignature(policyBase64);
	console.log(
		aliyunServerURL,
		"aliyunServerURL=====",
		params.filePath,
		" params.filePath"
	);
	wx.uploadFile({
		url: aliyunServerURL,
		filePath: params.filePath,
		name: "file",
		formData: {
			key: aliyunFileKey,
			policy: policyBase64,
			OSSAccessKeyId: accessid,
			signature: signature,
			success_action_status: "200",
		},
		success: function (res) {
			console.log(res, "resresres");
			if (res.statusCode != 200) {
				if (params.fail) {
					params.fail(res);
				}
				return;
			}
			if (params.success) {
				params.success(aliyunFileKey);
			}
		},
		fail: function (err) {
			err.wxaddinfo = aliyunServerURL;
			if (params.fail) {
				params.fail(err);
			}
		},
		complete: function () {
			if (params.complete) {
				params.complete();
			}
		},
	});
};

const getPolicyBase64 = function () {
	let date = new Date();
	date.setHours(date.getHours() + env.timeout);
	let srcT = date.toISOString();
	const policyText = {
		expiration: srcT, //设置该Policy的失效时间
		conditions: [
			["content-length-range", 0, 5 * 1024 * 1024], // 设置上传文件的大小限制,5mb
		],
	};

	const policyBase64 = Base64.encode(JSON.stringify(policyText));
	return policyBase64;
};

const getSignature = function (policyBase64) {
	const accesskey = env.AccessKeySecret;

	const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
		asBytes: true,
	});
	const signature = Crypto.util.bytesToBase64(bytes);

	return signature;
};

module.exports = uploadFile;
